package com.hqully.smartfumehood.udp;

import com.hqully.smartfumehood.udp.handle.UDPServerHandler;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;
import lombok.extern.slf4j.Slf4j;
/**
 * <p>
 *     UDP服务类
 * </p>
 * @author liulingyu
 * @date 2022-05-12 17:45
 * @version 1.0
 */
@Slf4j
public class UDPServer {
    public void bind(int port){
        EventLoopGroup bossGroup=new NioEventLoopGroup();

        try {
            Bootstrap serverBootstrap = new Bootstrap()
                    .group(new NioEventLoopGroup())
                    .channel(NioDatagramChannel.class)
                    .option(ChannelOption.SO_BROADCAST, true)
                    .handler(new UDPServerHandler());

            ChannelFuture future = serverBootstrap.bind(port).sync();


            log.info(UDPServer.class.getName()+" started and listend on "+future.channel().localAddress());

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
        }
    }
}
